<?php

namespace app\controllers\admin\uni\supplier;

use app\controllers\admin\base\AdminController;
use app\models\SupplierAddress;
use app\models\SupplierCategory as Model;
use app\models\SupplierImageCategory;

class CategoryController extends AdminController
{
    public function actionList()
    {
        $page = intval($this->post('page', $this->page));
        $page_size = intval($this->post('pageSize', $this->pageSize));

        $query = Model::find()
            ->orderBy(['sort' => SORT_ASC, 'id' => SORT_ASC])
            ->where(['!=', 'status', -99]);

        $totalCount = $query->count();
        $totalPage = ceil($totalCount / $page_size);

        $list = $query->offset(($page - 1) * $page_size)
            ->with(['image.image'])
            ->limit($page_size)
            ->asArray()
            ->all();

        return $this->success([
            $this->totalCount   => $totalCount,
            $this->totalPage    => $totalPage,
            $this->list         => $list,
        ]);
    }

    // 上传图片
    public function actionEditImage()
    {
        $id = $this->post('id');
        $image_id = $this->post('image_id');

        if ($id && $image_id) {
            $model = new SupplierImageCategory();
            $model->image_id = $image_id;
            $model->supplier_category_id = $id;
            $model->save();
            return $this->success();
        }
    }

    public function actionEdit()
    {
        $data = $this->post();

        try {
            if (isset($data['id']) && $data['id'] > 0) {
                $model = Model::findOne(['id' => $data['id']]);
                if (!$model) {
                    return $this->fail('操作失败1');
                }
            } else {
                $model = new Model();
            }
            $data = ['SupplierCategory' => $data];
            if ($model->load($data) && $model->save()) {
                return $this->success($model, '操作成功');
            }

        } catch (\Exception $e) {
            return $this->fail($e->getMessage());
        }
    }

    public function actionRemove()
    {
        $id = $this->post('id');

        try {
            $model = Model::findOne(['id' => $id]);
            $model->status = -99;
            $model->save();
            return $this->success($model, '删除成功');
        } catch (\Exception $e) {
            return $this->fail($e->getMessage());
        }
    }

    public function actionChangeStatus()
    {
        $id = intval($this->post('id'));
        $status = intval($this->post('status'));

        try {
            $model = Model::findOne(['id' => $id]);
            $model->status = $status;
            $model->save();

            return $this->success();
        } catch (\Exception $e) {
            return $this->fail('操作错误: ' . $e->getMessage());
        }
    }

    public function actionChangeSort()
    {
        $id = intval($this->post('id'));
        $sort = intval($this->post('sort'));

        try {
            $model = Model::findOne(['id' => $id]);
            $model->sort = $sort;
            $model->save();

            return $this->success();
        } catch (\Exception $e) {
            return $this->fail('操作错误: ' . $e->getMessage());
        }
    }

    public function actionBatchRemove()
    {
        $ids = $this->post('ids');

        try {
            Model::updateAll(['status' => -99], ['id' => $ids]);

            return $this->success();
        } catch (\Exception $e) {
            return $this->fail('删除错误: ' . $e->getMessage());
        }
    }
}